package com.zgzg.util.comm;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class CreateHTML {

	   /**
     * 生成静态页面主方法
     * @param context ServletContext
     * @param data 一个Map的数据结果集
     * @param templatePath ftl模版路径
     * @param targetHtmlPath 生成静态页面的路径
     */
    public static void crateHTML(ServletContext context,Map<String,Object> data,String templatePath,String targetHtmlPath){
        Configuration freemarkerCfg = new Configuration();
        //加载模版
        freemarkerCfg.setServletContextForTemplateLoading(context, "/");
        freemarkerCfg.setEncoding(Locale.getDefault(), "utf-8");
        freemarkerCfg.setDefaultEncoding("utf-8");
       // freemarkerCfg.setOutputEncoding("utf-8");
        try {
            //指定模版路径
            Template template = freemarkerCfg.getTemplate(templatePath,"utf-8");
            //template.setEncoding("utf-8");
            //静态页面路径
            String htmlPath = context.getRealPath("pages/html")+"/"+targetHtmlPath;
            File htmlFile = new File(htmlPath);
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "utf-8"));
            //处理模版  
            template.process(data, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}
